//
//  VerificationView.m
//  商城
//
//  Created by runone02 on 2018/8/29.
//  Copyright © 2018年 liqiyun. All rights reserved.
//

#import "VerificationView.h"

@interface VerificationView()<UITextFieldDelegate>
/** */
@property (nonatomic,strong) UIImageView *accountImage;
/** */
@property (nonatomic,strong) UIImageView *pasdImage;
/** 账号*/
@property (nonatomic,strong) UITextField *accoutTF;
/** 账号*/
@property (nonatomic,strong) UITextField *passdTF;
/** 验证码*/
@property (nonatomic,strong) UIButton *codeButton;

/** 协议*/
@property (nonatomic,strong) UIButton *protocolButton;
@end

@implementation VerificationView


- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.userInteractionEnabled = YES;
    
    self.accountImage = [[UIImageView alloc]init];
    self.accountImage.image = IMAGE(@"myb_set_kaihu");
    [self addSubview:self.accountImage];
    [self.accountImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).with.offset(10);
        make.left.mas_equalTo(self).with.offset(10);
        make.width.height.mas_equalTo(20);
    }];
    
    self.accoutTF = [[UITextField alloc]init];
    self.accoutTF.placeholder = @"请输入账号";
    self.accoutTF.userInteractionEnabled = YES;
    self.accoutTF.font = TITLE_FONT(30);
    [self addSubview:self.accoutTF];
    [self.accoutTF mas_makeConstraints:^(MASConstraintMaker *make){
        make.centerY.mas_equalTo(self.accountImage);
        make.left.mas_equalTo(self.accountImage.mas_right).with.offset(10);
        make.right.mas_equalTo(self).with.offset(10);
        make.height.mas_equalTo(30);
    }];
    
    UIView *lineOneView = [[UIView alloc]init];
    lineOneView.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:lineOneView];
    [lineOneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.accoutTF.mas_bottom).with.offset(5);
        make.left.mas_equalTo(self).with.offset(10);
        make.right.mas_equalTo(self).with.offset(-10);
        make.height.mas_equalTo(1);
    }];
    
    self.pasdImage = [[UIImageView alloc]init];
    self.pasdImage.image = IMAGE(@"myb_set_password");
    [self addSubview:self.pasdImage];
    [self.pasdImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(lineOneView.mas_bottom).with.offset(30);
        make.left.mas_equalTo(self).with.offset(10);
        make.width.height.mas_equalTo(20);
    }];
    
  
    
    self.codeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.codeButton.backgroundColor = [UIColor lightGrayColor];
    [self.codeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    [self.codeButton addTarget:self action:@selector(codeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.codeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.codeButton.titleLabel.font = TITLE_FONT(26);
    [self addSubview:self.codeButton];
    [self.codeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.pasdImage);
        make.width.mas_equalTo(SCREEN_WIDTH/5);
        make.right.mas_equalTo(self).with.offset(-10);
        make.height.mas_equalTo(30);
    }];
    
    self.passdTF = [[UITextField alloc]init];
    self.passdTF.userInteractionEnabled = YES;
    self.passdTF.font = TITLE_FONT(30);
    self.passdTF.placeholder = @"请输入密码";
    [self addSubview:self.passdTF];
    [self.passdTF mas_makeConstraints:^(MASConstraintMaker *make){
        make.centerY.mas_equalTo(self.pasdImage);
        make.left.mas_equalTo(self.pasdImage.mas_right).with.offset(10);
        make.right.mas_equalTo(self.codeButton.mas_left).with.offset(-10);
        make.height.mas_equalTo(30);
    }];
    
    UIView *lineTowView = [[UIView alloc]init];
    lineTowView.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:lineTowView];
    [lineTowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.passdTF.mas_bottom).with.offset(5);
        make.left.mas_equalTo(self).with.offset(10);
        make.right.mas_equalTo(self).with.offset(-10);
        make.height.mas_equalTo(1);
    }];
    
   
    
    self.loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.loginButton setTitle:@"登录" forState:UIControlStateNormal];
    [self.loginButton addTarget:self action:@selector(loginButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.loginButton.backgroundColor = [UIColor lightGrayColor];
    self.loginButton.titleLabel.font = TITLE_FONT(36);
    
    [self.loginButton.layer setMasksToBounds:YES];
    [self.loginButton.layer setCornerRadius:1.0];
    
    [self.loginButton.layer setBorderWidth:1.0];
    self.loginButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
    
    
    [self addSubview:self.loginButton];
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).with.offset(20);
        make.right.mas_equalTo(self).with.offset(-20);
        make.top.mas_equalTo(lineTowView.mas_bottom).with.offset(30);
        
        make.height.mas_equalTo(50);
    }];
    
    self.protocolButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.protocolButton setTitle:@"登录成功即代表同意《服务协议》，若本/n号码还未注册请点击前往注册" forState:UIControlStateNormal];
    self.protocolButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [self.protocolButton addTarget:self action:@selector(protocolButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.protocolButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    self.protocolButton.titleLabel.font = TITLE_FONT(26);
    [self addSubview:self.protocolButton];
    [self.protocolButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).with.offset(10);
        make.top.mas_equalTo(self.loginButton.mas_bottom).with.offset(20);
        make.right.mas_equalTo(self).with.offset(-10);
        make.height.mas_equalTo(20);
    }];
}


- (void)protocolButtonClick:(UIButton *)btn{
    NSLog(@"遵守协议");
}

- (void)loginButtonClick:(UIButton *)btn{
    NSLog(@"登录");
}


//获取验证码
- (void)codeButtonClick:(UIButton *)codeButton{
    __block NSInteger time = 59; //设置倒计时时间
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0));
    
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0 * NSEC_PER_SEC, 0); //每秒执行
    WEAKSELF
    dispatch_source_set_event_handler(_timer, ^{
        
        if(time <= 0){ //倒计时结束，关闭
            
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置按钮的样式
                [weakSelf.codeButton setTitle:@"重新发送" forState:UIControlStateNormal];
                weakSelf.codeButton.userInteractionEnabled = YES;
            });
            
        }else{
            
            NSInteger seconds = time % 60;
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置按钮显示读秒效果
                [weakSelf.codeButton setTitle:[NSString stringWithFormat:@"重新发送(%.2ld)", (long)seconds] forState:UIControlStateNormal];
                weakSelf.codeButton.userInteractionEnabled = NO;
            });
            time--;
        }
    });
    dispatch_resume(_timer);
}

@end
